<?php
namespace Plu\Library\Services\Tga\Messageboard;

use Plu\Library\Services\ServiceBase;

class RedisService extends ServiceBase {
    const KEY_OF_APP_INFO = "message:app:%s";

    const KEY_OF_FORM_INFO = "message:form:%s";

    const KEY_OF_MESSAGE    = "message:app:user:%s:%s";

    const KEY_OF_MESSAGE_NODATE    = "message:app:user:%s";

    const KEY_OF_ADMIRE   = "admire:app:user:%s:%s";

    const KEY_OF_MESSAGE_TIME = "message:app:user:time:%s";

    public function __construct() {
        $this->_redis = $this->getRedis('webcache');
    }

    public function getAppInfo($appkey) {
        return json_decode($this->_redis->get(sprintf(self::KEY_OF_APP_INFO, $appkey)), true);
    }

    /**
     *  如果用户发送留言 把信息存放到 message:$appkey 这个hash中。
     */
    public function setnodate($suffix, $uid, $data){
        $key = sprintf(self::KEY_OF_MESSAGE_NODATE, $suffix);

        return $this->_redis->hSet($key, $uid, $data);
    }

    /**
     *  如果用户发送留言 把信息存放到 message:$appkey 这个hash中。
     */
    public function setMessage($suffix, $date, $uid, $data){
        $key = sprintf(self::KEY_OF_MESSAGE, $suffix, $date);

        return $this->_redis->hSet($key, $uid, $data);
    }

    /**
     *  获取用户留言信息
     */
    public function getMessage($suffix, $date, $uid){
        $key = sprintf(self::KEY_OF_MESSAGE, $suffix, $date);

        return $this->_redis->hGet($key, $uid);
    }


    public function incrMessage($suffix, $date, $uid){
        $key = sprintf(self::KEY_OF_MESSAGE, $suffix, $date);

        return $this->_redis->hIncrBy($key, $uid, 1);
    }

    public function setAdmire($suffix, $date, $field, $data){
        $key = sprintf(self::KEY_OF_ADMIRE, $suffix, $date);

        return $this->_redis->hSet($key, $field, $data);
    }

        
    public function getAdmire($suffix, $date, $field){
        $key = sprintf(self::KEY_OF_ADMIRE, $suffix, $date);
        return $this->_redis->hGet($key, $field);
    }


    public function incrAdmire($suffix, $date, $field){
        $key = sprintf(self::KEY_OF_ADMIRE, $suffix, $date);

        return $this->_redis->hIncrBy($key, $field, 1);
    }


/*    public function del($suffix, $uid){
        $key = sprintf(self::KEY_OF_MESSAGE, $suffix);

        return $this->_redis->hDel($key, $uid);
    }*/

        /**
     *  获取用户留言信息
     */
/*    public function getAll($suffix){
        $key = sprintf(self::KEY_OF_MESSAGE, $suffix);

        return $this->_redis->hGetAll($key);
    }*/
}
?>
